<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2019/3/16 0016
 * Describe: 磨盘风控定时任务类
 */

namespace app\index\controller\mopan;

use app\index\controller\Order;
use think\Controller;
use think\Log;

class MoPanTask extends Controller
{
    public function __construct()
    {
        //日志初始化保存地址
        $requestName = Request::instance()->module();
        $controllerName = Request::instance()->controller();
        $setPath = strtolower("{$requestName}/{$controllerName}/");
        Log::init(['path'=>LOG_PATH.$setPath,'type'  => 'file',]);
    }

    /**
     * 放款记录上传至磨盘 [放款记录上传API] [定时任务]
     */
    public function loanRecordUp()
    {
        //获取昨天放款用户信息
        $data = $this->getLastLanoUserInfo();
        $result = (new MoPan())->loanRecordUp($data);
        $result = json_decode($result);
        \Think\Log::record('昨天放款用户信息上传磨盘后 磨盘返回值'.$result);
        if($result['000000']){
            \Think\Log::record('昨天用户放款'.date("Y-m-d",strtotime("-1 day")).'数据上传磨盘失败');
        }else{
            \Think\Log::record('昨天用户放款'.date("Y-m-d",strtotime("-1 day")).'数据上传磨盘成功');
        }
    }

    /**
     * 还款记录上传至磨盘 [还款记录上传API] [定时任务]
     */
    public function repaymentRecordUp()
    {
        //获取昨天还款的用户信息
        $data = $this->getLastRepaymentUserInfo();
        $result = (new MoPan())->loanRecordUp($data);
        $result = json_decode($result);
        \Think\Log::record('昨天还款用户信息上传磨盘后 磨盘返回值'.$result);
        if($result['000000']){
            \Think\Log::record('昨天用户还款'.date("Y-m-d",strtotime("-1 day")).'数据上传磨盘失败');
        }else{
            \Think\Log::record('昨天用户还款'.date("Y-m-d",strtotime("-1 day")).'数据上传磨盘成功');
        }
    }

    /**
     * 获取昨天放款的所有用户数据
     * @return array
     */
    private function getLastLanoUserInfo()
    {
        $data = db('loans_applylist')->alias('a')
            ->join('xb_mem_info m','a.UserID = m.ID')
            ->whereTime('OpenTime', 'yesterday')
            ->select();
        if(!$data){
            \Think\Log::record('昨天'.date("Y-m-d",strtotime("-1 day")).'没有还款用户');
        }
        $temp = [];
        foreach ($data as $k=>$v){
            $temp[$k] = [
                'realName'            => $v['TrueName'],     //身份证名称
                'identityNo'          => $v['IDCard'],       //身份证号码
                'mobile'              => $v['Mobile'],       //手机号
                'tradeNo'             => $v['OrderSn'],      //订单编号
                'applyDate'           => $v['ApplyTime'],    //申请时间
                'loanDate'            => $v['OpenTime'],     //放款时间
                'loanAmount'          => $v['ApplyMoney'],   //申请金额
                'shouldPayAmount'     => $v['AdoptMoney'],   //还款金额
                'interest'            => $v['yuqililv'],     //利息
                'loanRate'            => $v['yuqililv']/100, //利率
                'totalTerm'           => 1,                  //还款期数
                'targetRepayDateType' => 1,                  //账单日类型
                'termPeriod'          => $v['ApplyDay'],     //每期贷款 周期
                'targetRepayDateList' => $v['YyFkTime']      //首次还款日
            ];
        }
        return $temp;
    }

    /**
     * 获取昨天还款的用户信息
     * @return array
     */
    private function getLastRepaymentUserInfo()
    {
        //获取昨天还款的用户信息
        $data = db('loans_applylist')->alias('a')
            ->join('xb_loans_hklist h','a.OrderSn = h.OrderSn')
            ->whereTime('SjFkTime','yesterday')
            ->field('*,sum(h.TotalMoney) as SjTotalMoney,h.ID as HID,a.ID as ID')
            ->select();
        if(!$data){
            \Think\Log::record('昨天'.date("Y-m-d",strtotime("-1 day")).'没有还款用户');
        }
        $temp = [];
        foreach ($data as $k=>$v){
            //获取订单罚息
            $orderPenalty = (new Order())->getOrderPenalty($v);
            $temp[$k] = [
                'tradeNo'              => $v['OrderSn'],     //订单编号
                'payOffDate'           => $v['SjFkTime'],    //实际还款时间
                'realReplayment'       => $v['SjTotalMoney'],//实际还款金额
                'overdueManagementFee' => $v['Interest'],    //贷后管理费
                'overdueInterestRate'  => $v['yuqililv']/100,//贷后逾期罚息利率
                'overdueFee'           => $orderPenalty,     //逾期罚息
            ];
        }
        return $temp;
    }


}